package hip.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * properties util
 */
public class PropertiesUtil {
    private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

    private static Map<String, Properties> PROPERTIES_MAP = new ConcurrentHashMap<String, Properties>();

    public static Properties getProperties(String fileKey) {
        if (PROPERTIES_MAP.containsKey(fileKey)) {
            return PROPERTIES_MAP.get(fileKey);
        }
        Properties properties = new Properties();
        try {
            InputStream input = PropertiesUtil.class.getResourceAsStream(fileKey);
            InputStreamReader in = new InputStreamReader(input, "UTF-8");
            properties.load(in);
        } catch (IOException e) {
            throw new RuntimeException("解析配置文件出错", e);
        }
        PROPERTIES_MAP.put(fileKey, properties);
        return properties;
    }

    public static Properties getAbsolutePathProperties(String filePath) {
        if (PROPERTIES_MAP.containsKey(filePath)) {
            return PROPERTIES_MAP.get(filePath);
        }
        Properties properties = new Properties();
        InputStream input = null;
        InputStreamReader in = null;
        File file = new File(filePath);
        try {
            if (file.exists()) {
                input = new FileInputStream(file);
                in = new InputStreamReader(input, "UTF-8");
            }
            if (input == null) return null;
            properties.load(in);
        } catch (Exception e) {
            throw new RuntimeException("解析配置文件出错", e);
        }

        PROPERTIES_MAP.put(filePath, properties);
        return properties;
    }


    private static Properties properties = null;

    public static Properties newInstance() {
        properties = new Properties();
        InputStream in = null;
        try {
            in = PropertiesUtil.class.getClassLoader().getResourceAsStream("log4jdb.properties");
            properties.load(in);
        } catch (IOException e) {

        } finally {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        return properties;
    }

    /**
     * load prop file
     *
     * @param propertyFileName
     * @return
     */
    public static Properties loadProperties(String propertyFileName) {
        Properties prop = new Properties();
        InputStream in = null;
        try {
            ClassLoader loder = Thread.currentThread().getContextClassLoader();
            URL url = loder.getResource(propertyFileName); // 方式1：配置更新不需要重启JVM
            if (url != null) {
                //in = new FileInputStream(url.getPath());
                in = loder.getResourceAsStream(propertyFileName); // 方式2：配置更新需重启JVM
                if (in != null) {
                    prop.load(in);
                }
            }
        } catch (IOException e) {
            logger.error("load {} error!", propertyFileName);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    logger.error("close {} error!", propertyFileName);
                }
            }
        }
        return prop;
    }

    public static Properties loadFileProperties(String propertyFileName) {
        Properties prop = new Properties();
        InputStream in = null;
        try {
            URL url = new File(propertyFileName).toURI().toURL();
            in = new FileInputStream(url.getPath());
            if (in != null) {
                prop.load(in);
            }
        } catch (IOException e) {
            logger.error("load {} error!", propertyFileName);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    logger.error("close {} error!", propertyFileName);
                }
            }
        }
        return prop;
    }

    /**
     * load prop value of string
     *
     * @param key
     * @return
     */
    public static String getString(Properties prop, String key) {
        return prop.getProperty(key);
    }

    /**
     * load prop value of int
     *
     * @param key
     * @return
     */
    public static int getInt(Properties prop, String key) {
        return Integer.parseInt(getString(prop, key));
    }

    /**
     * load prop value of boolean
     *
     * @param prop
     * @param key
     * @return
     */
    public static boolean getBoolean(Properties prop, String key) {
        return Boolean.valueOf(getString(prop, key));
    }

}
